package com.yetong.domain.strategy.service.rule.chain.impl;

import com.yetong.domain.strategy.repository.IStrategyRepository;
import com.yetong.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.yetong.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import com.yetong.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;

/**
 * 作者：叶童
 * 时间：2025/5/5
 * 黑名单方法
 */
@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    private IStrategyRepository repository;

    @Override
    public  DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-黑名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        // 查询规则值配置
       String ruleValue= repository.queryStrategyRuleValue(strategyId,ruleModel());
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(splitRuleValue[0]);
        //100:user001,user002,user003
        // 黑名单抽奖判断
        String[] userBlackIds = splitRuleValue[1].split(Constants.SPLIT);
        for (String userBlackId : userBlackIds){
            if(userId.equals(userBlackId)){
                log.info("抽奖责任链-黑名单接管 userId: {} strategyId: {} ruleModel: {} awardId:{}", userId, strategyId, ruleModel(),awardId);
               return  DefaultChainFactory.StrategyAwardVO.builder()
                       .awardId(awardId)
                       .logicModel(ruleModel())
                       .build();
            }

        }
        // 过滤其他责任链
        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel());
        //放行
        return next().logic(userId, strategyId);
    }

    @Override
    protected String ruleModel() {
        return DefaultChainFactory.LogicModel.RULE_BLACKLIST.getCode();
    }
}
